﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WhereAmiMvc.Dbs;
using WhereAmiMvc.Entities;

namespace WhereAmiMvc.Controllers
{
    public class DeptController : Controller
    {
        // GET: Dept
        public ActionResult Index()
        {
            DeptBll bll = new DeptBll();
            var list = bll.GetAll();

            return View(list);
        }

        // GET: Dept/Details/5
        public ActionResult Details(string id)
        {
            DeptBll bll = new DeptBll();
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // GET: Dept/Create
        public ActionResult Create()
        {
            Dept obj = new Dept();

            return View(obj);
        }

        // POST: Dept/Create
        [HttpPost]
        public ActionResult Create(Dept obj)
        {
            try
            {
                // TODO: Add insert logic here
                DeptBll bll = new DeptBll();

                obj.IdDept = Commons.GenerateIdByForeTag("BM");

                bll.Insert(obj);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Dept/Edit/5
        public ActionResult Edit(string id)
        {
            DeptBll bll = new DeptBll();
            var obj = bll.GetByKey(id);


            return View(obj);
        }

        // POST: Dept/Edit/5
        [HttpPost]
        public ActionResult Edit(Dept dept)
        {
            try
            {
                // TODO: Add update logic here
                DeptBll bll = new DeptBll();
                var obj = bll.Update(dept);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Dept/Delete/5
        public ActionResult Delete(string  id)
        {
            DeptBll bll = new DeptBll();
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // POST: Dept/Delete/5
        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                DeptBll bll = new DeptBll();

                bll.DeleteByKey(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}